<template>
    <el-dialog
            :title="!dataForm.adjustmentId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                <el-form-item label="调整类型" prop="adjustmentType">
                    <el-select v-model="dataForm.adjustmentType" placeholder="请选择调整类型">
                        <el-option label="车辆" value="vehicle"></el-option>
                        <el-option label="司机" value="driver"></el-option>
                        <el-option label="承运商" value="carrier"></el-option>
                        <el-option label="路线" value="route"></el-option>
                        <el-option label="时间表" value="schedule"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="调整人" prop="adjustedBy">
                        <el-input v-model="dataForm.adjustedBy" placeholder="调整人"></el-input>
                </el-form-item>
                <el-form-item label="调整时间" prop="adjustedAt">
                        <el-date-picker
                                v-model="dataForm.adjustedAt"
                                type="datetime"
                                placeholder="调整时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="调整详情" prop="adjustmentDetails">
                        <el-input v-model="dataForm.adjustmentDetails" placeholder="调整详情"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            adjustmentId: null,
            planId: null,
            taskId: null,
            adjustmentType: null,
            adjustedBy: null,
            adjustedAt: null,
            adjustmentDetails: null
          },
          dataRule: {
            adjustmentId: [
                        { required: true, message: '调整记录唯一标识不能为空', trigger: 'blur' }
            ],
            planId: [
                        { required: true, message: '运输计划ID不能为空', trigger: 'blur' }
            ],
            taskId: [
                        { required: true, message: '运输任务ID不能为空', trigger: 'blur' }
            ],
            adjustmentType: [
                        { required: true, message: '调整类型（车辆、司机、承运商、路线、时间表）不能为空', trigger: 'blur' }
            ],
            adjustedBy: [
                        { required: true, message: '调整人不能为空', trigger: 'blur' }
            ],
            adjustedAt: [
                        { required: true, message: '调整时间不能为空', trigger: 'blur' }
            ],
            adjustmentDetails: [
                        { required: true, message: '调整详情不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.adjustmentId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.adjustmentId = id || 0
            if (this.dataForm.adjustmentId) {
              this.$http({
                url: this.$http.adornUrl(`/app/transport_plan_adjustments/info/${this.dataForm.adjustmentId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.apptransportplanadjustments
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/transport_plan_adjustments/${!this.dataForm.adjustmentId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>